from flask import Flask,jsonify
import os
import json
from flask_cors import CORS
import demo

app = Flask(__name__)
CORS(app)


@app.route('/fenggeList',methods=['GET', 'POST'])
def display_fenggeList():  # put application's code here
    df = demo.query_wencai('价值', loop=True)
    df = json.loads(df)[:2]
    examples1 = []
    for item in df:
        examples1.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })
    df = demo.query_wencai('平衡', loop=True)
    df = json.loads(df)[:2]
    examples2 = []
    for item in df:
        examples2.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })
    df = demo.query_wencai('成长', loop=True)
    df = json.loads(df)[:2]
    examples3 = []
    for item in df:
        examples3.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })
    df = demo.query_wencai('白马型', loop=True)
    df = json.loads(df)[:2]
    examples4 = []
    for item in df:
        examples4.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })

    content = [{
        "name": "积极型",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples1
    },{
        "name": "均衡型",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples2
    },{
        "name": "成长型",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples3
    },{
        "name": "白马型",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples4
    }]
    print(content)
    return jsonify(content),200

@app.route('/zhutiList',methods=['GET', 'POST'])
def display_zhutiList():  # put application's code here
    df = demo.query_wencai('沪深300', loop=True)
    df = json.loads(df)[:2]
    examples1 = []
    for item in df:
        examples1.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })
        
    df = demo.query_wencai('上证50板块', loop=True)
    df = json.loads(df)[:2]
    examples2 = []
    for item in df:
        examples2.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })
        
    df = demo.query_wencai('金融', loop=True)
    df = json.loads(df)[:2]
    examples3 = []
    for item in df:
        examples3.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })
    
    df = demo.query_wencai('科技', loop=True)
    df = json.loads(df)[:2]
    examples4 = []
    for item in df:
        examples4.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })

    content = [{
        "name": "沪深300",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples1
    },{
        "name": "上证50板块",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples2
    },{
        "name": "金融",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples3
    },{
        "name": "科技",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples4
    }]
    print(content)
    return jsonify(content),200

@app.route('/celveList',methods=['GET', 'POST'])
def display_celveList():  # put application's code here
    df = demo.query_wencai('适合长期持有', loop=True)
    df = json.loads(df)[:2]
    examples1 = []
    for item in df:
        examples1.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })
        
    df = demo.query_wencai('高频交易', loop=True)
    df = json.loads(df)[:2]
    examples2 = []
    for item in df:
        examples2.append({
            'name': item['股票简称'],
            "annualReturns": item['最新涨跌幅']
            })
        
    # df = demo.query_wencai('套利', loop=True)
    # df = json.loads(df)[:2]
    # examples3 = []
    # for item in df:
    #     examples3.append({
    #         'name': item['股票简称'],
    #         "annualReturns": item['最新涨跌幅']
    #         })

    content = [{
        "name": "长期持有策略",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples1
    },{
        "name": "高频交易策略",
        "maxAnnualReturns": "22.89%",
        "tags": ["中收益", "高风险"],
        "examples":examples2
    }]
    print(content)
    return jsonify(content),200

@app.route('/query',methods=['POST'])
def display_result():
    with open(os.path.join(os.path.dirname(__file__), 'db.json'), "r", encoding="utf-8") as f:
        content = json.load(f)
    content = content['fenggeList']
    return jsonify(content),200

if __name__ == '__main__':
    app.run(port=8081,debug=True,host='localhost')